<?php
App::import('Vendor', 'Amf.AmfCallback');

class Post {

	var $_remoteAlias = 'models.Post';
}

class AmfCallbackTest extends CakeTestCase {

	function start() {
		parent::start();
		$this->AmfCallback = new AmfCallback(new stdClass);
	}

	function endTest($method) {
		parent::endTest($method);
		ClassRegistry::flush();
	}

	function testCallbackInstance() {
		$this->assertTrue(is_a($this->AmfCallback, 'AmfCallback'));
	}

	var $data = array();

	function recursive(&$data) {
		$out = array();
		$result = null;
		if (is_object($data)) {
			$array = get_object_vars($data);
			foreach ($array as $key => $value) {
				if (is_object($value)) {
					$out[$key] = $this->recursive($value);
				} else if (is_array($value)) {
					$out[$key] = $this->recursive($value);
				}
			}

			if (empty($out)) {
				$out = $this->AmfCallback->encode($data);
			}

		} else if (is_array($data)) {
			return $data;
		}
		return $out;
	}

	function testEncode() {

		$testData = array('post' =>
			array('Post' => array(
				'id' => 1,
				'title' => 'test title',
				'created' => date('Y-m-d H:m:s', strtotime('now'))
				)
			)
		);


		$data = $test = Set::map(array('Result' => $testData));

		$result = $this->AmfCallback->encode($data);
		$expected = array($data, 3, 'ResultEvent');
		$this->assertEqual($expected, $result);

		$result = $this->AmfCallback->encode($data->post);

		$expected = array($test->post->Post, 3, 'Post');
		$this->assertEqual($expected, $result);

		ClassRegistry::addObject('post', new Post);

		$result = $this->AmfCallback->encode($data->post);

		$expected = array($test->post, 3, 'models.Post');

		$this->assertEqual($expected, $result);
	}

	function testRecursiveEncode() {

		$testData = array('post' =>
			array(
				'Post' => array(
					'id' => 1,
					'title' => 'test title',
					'created' => date('Y-m-d H:m:s', strtotime('now'))
				),
				'User' => array(
					'id' => 1,
					'username' => 'john',
					'created' => date('Y-m-d H:m:s', strtotime('now'))
				),
			)
		);

		$data = Set::map(array('Result' => $testData));

		$expected = array($data->post, 3, 'Array');

		$result = $this->AmfCallback->encode($data->post);
		$this->assertEqual($expected, $result);


		ClassRegistry::addObject('post', new Post);

		$expected = array($data->post->Post, 3, 'models.Post');

		$result = $this->AmfCallback->encode($data->post->Post);

		$this->assertEqual($expected, $result);


	}

	function testErrorMessage() {
		$data = Set::map(array('Error' =>
			array('Error' => array(
				'body' =>
					array('users'=> array(
						array('User' => array('id'=> 1)),
						array('User' => array('id'=> 2))
					)),
				'string' => 'this is the error message'
			))
		));
		$result = $this->AmfCallback->encode($data);
		$error = new ErrorMessage($data->Error);
		//have to unset cause they are autogenerated UUID
		unset($result[0]->correlationId, $result[0]->messageId, $result[0]->clientId);
		unset($error->_type, $error->correlationId, $error->messageId, $error->clientId);

		$expected = array($error, 3, 'flex.messaging.messages.ErrorMessage');
		$this->assertEqual($expected, $result);
	}
}